/*==============================================================================
FILE NAME: Figure_4.do
CREATED: 12 June 2025
==============================================================================*/

**Figure 4

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

//Panel A: NOV
set scheme modern
* Load air panel data
use "$processed_data/Air_Panel.dta", clear
drop if never_air_inv == 1

* Create time variable
egen t = group(year month) 
xtset RN_id t

* Generate difference variables
forv h = 0/12 {
	gen p_air_nov_`h' = f`h'.p_air_nov - l1.p_air_nov
}

forv h = 2/12 {
	gen p_air_nov_neg`h' = l`h'.p_air_nov - l1.p_air_nov
}

egen RN_year = group(RN year)
keep RN_id t RN_year p_air_nov_* p_air_incident

* Initialize containers for event study estimates
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0

* Run regressions for likeihood of NOV before and after a citizen complaint
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_`h'  p_air_incident, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_incident] if Years == `h'
replace se = _se[p_air_incident] if Years == `h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == `h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == `h'
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_neg`h'  p_air_incident, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_incident] if Years == -`h'
replace se = _se[p_air_incident] if Years == -`h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == -`h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == -`h'
}

* Export point estimates
preserve
keep if Years != .
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_4_Panel_A.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(gs10) fint(inten30) lwidth(0) lpattern(solid)) ///
    (line b Years, lcolor(gs3) lpattern(solid) lwidth(medium)) ///
    (line Zero Years, lcolor(gs8)), ///
    xlabel(-12(1)12, nogrid labsize(vlarge)) ///
    ylabel(-0.04(0.02)0.08, labsize(vlarge)) ///
    legend(off) ///
    ytitle("{&Delta} P(Notice of Violation)", size(vlarge)) ///
    xtitle("Month", size(vlarge)) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(8.6)


* Create figure
graph export "$figures/Figure_4_Panel_A.pdf", replace

restore

* Panel C: NOE
use "$processed_data/Air_Panel.dta", clear
drop if never_air_inv == 1

egen t = group(year month)
xtset RN_id t

* Create difference variables
forv h = 0/12 {
	gen p_air_noe_`h' = f`h'.p_air_noe - l1.p_air_noe
}

forv h = 2/12 {
	gen p_air_noe_neg`h' = l`h'.p_air_noe - l1.p_air_noe
}

egen RN_year = group(RN year)
keep RN_id t RN_year p_air_noe_* p_air_incident

* Initalize variables
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0

* Run regressions for likeihood of NOE before and after a citizen complaint
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_`h'  p_air_incident, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_incident] if Years == `h'
replace se = _se[p_air_incident] if Years == `h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == `h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == `h'
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_neg`h'  p_air_incident, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_incident] if Years == -`h'
replace se = _se[p_air_incident] if Years == -`h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == -`h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == -`h'
}
preserve

* Export point_estimates
keep if Years != .
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_4_Panel_C.csv", replace

* Create Figure
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(gs10) fint(inten30) lwidth(0) lpattern(solid)) ///
    (line b Years, lcolor(gs3) lpattern(solid) lwidth(medium)) ///
    (line Zero Years, lcolor(gs8)), ///
    xlabel(-12(1)12, nogrid labsize(vlarge)) ///
    ylabel(-0.02(0.01)0.02, labsize(vlarge)) ///
    legend(off) ///
    ytitle("{&Delta} P(Notice of Enforcement)", size(vlarge)) ///
    xtitle("Month", size(vlarge)) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(8.6)
graph export "$figures/Figure_4_Panel_C.pdf", replace

restore

* Panel B
use "$processed_data/Air_Panel_with_referred.dta", clear

keep RN_id t RN_year p_air_nov_* p_air_incident p_referred

* Drop missing outcomes
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
drop if p_air_nov_`h' == .
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
drop if p_air_nov_neg`h' == .
}

* Initialize containters for both incident and referral effects
cap drop b u d se b_ref se_ref u_ref d_ref Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0
gen b_ref = 0
gen se_ref = 0
gen u_ref = 0 
gen d_ref = 0

* Run regressions for likeihood of NOV before and after a citizen complaint, given complaint is from outside the TCEQ jurisdiction
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_`h' p_referred p_air_incident, absorb(RN_year t) cluster(RN_id )
unique t if e(sample)
replace b = _b[p_air_incident] if Years == `h'
replace se = _se[p_air_incident] if Years == `h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == `h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == `h'
replace b_ref = _b[p_referred] if Years == `h'
replace se_ref = _se[p_referred] if Years == `h'
replace u_ref = (_b[p_referred] + 1.96*_se[p_referred]) if Years == `h'
replace d_ref = (_b[p_referred] - 1.96*_se[p_referred]) if Years == `h'
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_neg`h' p_referred p_air_incident, absorb(RN_year t) cluster(RN_id)
unique t if e(sample)
replace b = _b[p_air_incident] if Years == -`h'
replace se = _se[p_air_incident] if Years == -`h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == -`h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == -`h'
replace b_ref = _b[p_referred] if Years == -`h'
replace se_ref = _se[p_referred] if Years == -`h'
replace u_ref = (_b[p_referred] + 1.96*_se[p_referred]) if Years == -`h'
replace d_ref = (_b[p_referred] - 1.96*_se[p_referred]) if Years == -`h'
}

* Export point estimates
preserve
keep if Years != .
keep b u d se b_ref se_ref u_ref d_ref Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_4_Panel_B.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u_ref d_ref Years, col(gs10) fint(inten30) lwidth(0) lpattern(solid)) ///
    (line b_ref Years, lcolor(gs3) lpattern(solid) lwidth(medium)) ///
    (line Zero Years, lcolor(gs8)), ///
    xlabel(-12(1)12, nogrid labsize(vlarge)) ///
    ylabel(-0.04(0.02)0.08, labsize(vlarge)) ///
    legend(off) ///
    ytitle("{&Delta} P(Notice of Violation)", size(vlarge)) ///
    xtitle("Month", size(vlarge)) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(8.6)
* Create figure
graph export "$figures/Figure_4_Panel_B.pdf", replace
restore

* Panel D
use "$processed_data/Air_Panel_with_referred.dta", clear
keep RN_id t RN_year p_air_noe_* p_air_incident p_referred

* Drop missing outcomes
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
drop if p_air_noe_`h' == .
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
drop if p_air_noe_neg`h' == .
}

* Initialize variables
cap drop b u d se b_ref se_ref u_ref d_ref Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0
gen b_ref = 0
gen se_ref = 0
gen u_ref = 0 
gen d_ref = 0

* Run regressions for likeihood of NOE before and after a citizen complaint, given complaint is from outside the TCEQ jurisdiction
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_`h' p_referred p_air_incident, absorb(RN_year t) cluster(RN_id )
unique t if e(sample)
replace b = _b[p_air_incident] if Years == `h'
replace se = _se[p_air_incident] if Years == `h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == `h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == `h'
replace b_ref = _b[p_referred] if Years == `h'
replace se_ref = _se[p_referred] if Years == `h'
replace u_ref = (_b[p_referred] + 1.96*_se[p_referred]) if Years == `h'
replace d_ref = (_b[p_referred] - 1.96*_se[p_referred]) if Years == `h'
}
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_neg`h' p_referred p_air_incident, absorb(RN_year t) cluster(RN_id)
unique t if e(sample)
replace b = _b[p_air_incident] if Years == -`h'
replace se = _se[p_air_incident] if Years == -`h'
replace u = (_b[p_air_incident] + 1.96*_se[p_air_incident]) if Years == -`h'
replace d = (_b[p_air_incident] - 1.96*_se[p_air_incident]) if Years == -`h'
replace b_ref = _b[p_referred] if Years == -`h'
replace se_ref = _se[p_referred] if Years == -`h'
replace u_ref = (_b[p_referred] + 1.96*_se[p_referred]) if Years == -`h'
replace d_ref = (_b[p_referred] - 1.96*_se[p_referred]) if Years == -`h'
}

* Export point estimates
preserve
keep if Years != .
keep b u d se b_ref se_ref u_ref d_ref Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_4_Panel_D.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u_ref d_ref Years, col(gs10) fint(inten30) lwidth(0) lpattern(solid)) ///
    (line b_ref Years, lcolor(gs3) lpattern(solid) lwidth(medium)) ///
    (line Zero Years, lcolor(gs8)), ///
    xlabel(-12(1)12, nogrid labsize(vlarge)) ///
    ylabel(-0.02(0.01)0.02, labsize(vlarge)) ///
    legend(off) ///
    ytitle("{&Delta} P(Notice of Enforcement)", size(vlarge)) ///
    xtitle("Month", size(vlarge)) ///
    graphregion(color(white)) ///
    plotregion(color(white)) ///
    xsize(8.6)

* Create Figure
graph export "$figures/Figure_4_Panel_D.pdf", replace
restore
